
package com.xinan.weixin.msg;

import com.jfinal.aop.Before;
import com.jfinal.core.BaseController;
import com.jfinal.core.Const;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.SearchSql;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;
import com.xinan.utils.Constants;
import com.xinan.utils.PropertiesHelper;
import com.xinan.weixin.resource.WxResource;
import org.apache.log4j.Logger;

import java.util.*;

public class WxMessageController extends BaseController {
    private static String url = "";


    static {
        Properties r = PropertiesHelper.loadProperty("/weixin.properties");
        if (!r.isEmpty()) {
            try {
                url = r.getProperty("wxmediaUrl");
            } catch (Exception e) {
                Logger.getLogger(WxMessageController.class).error("无法读取参数", e);
            }
        } else {
            Logger.getLogger(WxMessageController.class).error("无法读取 weixin.properties");
        }
    }
    public void index() {
        render("/WEB-INF/views/weixin/msg/wxMessage.html");
    }
/**
 * add
 */
    public void  newModes() {
        List<Record> urlresource =  WxResource.dao.findAlls("") ;

        String   id=getPara("id");
//    List<Record> newlist = Db.find("select  * from     where    id=  '" + id + "'");
        List<Record> newlist =  WxMessage.dao.findAlls(  " id=  '" + id + "'") ;
        List<Record> types =  Msgtype.dao.findAlls(  " yxbz=1") ;//type
        setAttr("newlist",newlist);
        setAttr("types",types);
        setAttr("urlresource",urlresource);
        setAttr("url",url);

        render("/WEB-INF/views/weixin/msg/form.html");
    }

    /**
     * 在增加了SearchSql拦截之后，就可以根据页面的配置信息
     * 自动装配查询语句，并放入request中，可以通过getAttr(Const.SEARCH_SQL)函数获取到
     * 根据实际需要自动组装到sql语句中
     */
    @Before(SearchSql.class)
	public void query() {
        int pageNumber = getParaToInt("page");
        int pageSize = getParaToInt("rows");
        String where = getAttr(Const.SEARCH_SQL);
        Page<WxMessage> page = WxMessage.dao.page(pageNumber, pageSize, where);
        Map root = new HashMap();
		// / jqgrid用 分页显示
		root.put("total", page.getTotalPage());
		root.put("page", page.getPageNumber());
		root.put("records", page.getTotalRow());
		root.put("rows", page.getList());
		renderJson(JsonKit.toJson(root));
	}
    /**
     * save
     * @param
     * @return
     */
	public void save() {
        String description=getPara("description");
        String yxbz=getPara("yxbz");
        System.out.println("==========="+yxbz);
        String where =  " description=  '"+description + "'";
        if(max(where)){
        	renderText("关键字重复！");
        }  else {
                if(getModel(WxMessage.class).set("yxbz", yxbz).save()){
                	renderText(Constants.SAVESUCCESS);
                }else{
                	renderText(Constants.SAVEFAILED);
                }
        }
        //更新缓存
        message();
    }
	
	/**
     * Post表单数据
     *
     * @param
     * @return
     */
	public void update() {
        String description=getPara("description") ;
        String id=getPara("id");
        String yxbz=getPara("yxbz");
        String where =" description=  '"+description + "' and id !='"+id +"'";
        if(max(where)){
            renderJson("关键字重复！");
        } else {
            if(getModel(WxMessage.class).set("yxbz", yxbz).update()){
            	renderText(Constants.EDITSUCCESS);
            }else{
            	renderText(Constants.EDITFAILED);
            }
        }
        //更新缓存
        message();

    }

    /**
     * 删除数据
     */
	public void delete() {
        if(WxMessage.dao.deleteById(getPara("id"))){
        	renderText(Constants.DELETESUCCESS);
        }else{
        	renderText(Constants.DELETEFAILED);
        }
        message();
	}

    private static void message(){
        //更新缓存
        List<Record>  _list = new ArrayList();
        _list   =WxMessage.dao.findAlls(  "") ;
        CacheKit.put("WxMessage", "WxMessage", _list);
    }
    /**
     * 关键字去重复
     * @param where
     * @return  boolean
     */
    private  boolean max(String where){
        List<Record> types =  WxMessage.dao.findAlls(where) ;//type
        if(types.size()>0){
            return true  ;
        } else {
            return false;
        }
    }
}

